package com.ps.service.impl;

import com.ps.pojo.Premission;
import com.ps.mapper.PremissionMapper;
import com.ps.pojo.User;
import com.ps.service.PremissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

/**
 * <p>
 * 功能表 服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2020-12-18
 */
@Service
public class PremissionServiceImpl extends ServiceImpl<PremissionMapper, Premission> implements PremissionService {


    private PremissionMapper premissionMapper;


    @Override
    public List<String> findPermCodeList(Integer roleId) {
        return premissionMapper.findPermCodeList(roleId);
    }

    @Override
    public Map<Premission, List<Premission>> searchMenuList() {
        Map<Premission, List<Premission>> map=new TreeMap<>((p1, p2)->p1.getId()-p2.getId());
        Subject subject= SecurityUtils.getSubject();
        User loginUser= (User) subject.getPrincipal();
        List<Premission> list= premissionMapper.findByParentId(null);
        for(Premission po1:list){
            if(subject.isPermitted(po1.getPermCode())){
                List<Premission> list2= premissionMapper.findByParentId(po1.getId());
                List<Premission> list3= list2.stream().filter(permission -> subject.isPermitted(permission.getPermCode()))
                        .collect(Collectors.toList());
                map.put(po1,list3);
            }

        }
        return map;
    }
}
